#include "include/db.h"

static MYSQL conn;
static int db_connected = 0;

// 计算文件的 MD5 值
void calculate_md5(const char *filename, char *result) {
    unsigned char md5_result[16];
    FILE *file = fopen(filename, "rb");
    if (!file) {
        perror("fopen");
        return;
    }

    MD5_CTX md5Context;
    MD5_Init(&md5Context);

    unsigned char buffer[1024];
    size_t bytesRead;
    while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) != 0) {
        MD5_Update(&md5Context, buffer, bytesRead);
    }

    MD5_Final(md5_result, &md5Context);
    char md5_str[33];
    for(int i=0;i<16;i++){
        sprintf(md5_str+i*2,"%02x",md5_result[i]);
    }
    strcpy(result,md5_str);
    fclose(file);
}

int db_init(const char *host, const char *user, const char *pass, const char *dbname) {
    mysql_init(&conn);
    if (!mysql_real_connect(&conn, host, user, pass, dbname, 0, NULL, 0)) {
        fprintf(stderr, "MySQL connection error: %s\n", mysql_error(&conn));
        return -1;
    }
    db_connected = 1;
    return 0;
}

void db_close() {
    if (db_connected) {
        mysql_close(&conn);
        db_connected = 0;
    }
}
//注册新用户
int db_store_user(const char *user, const char *salt, const char *pass) {
    char query[512];
    snprintf(query, sizeof(query),
        "INSERT INTO users (username, salt, encrypted_pass,is delete) VALUES ('%s', '%s', '%s',0) ON DUPLICATE KEY UPDATE salt='%s', encrypted_pass='%s'",
        user, salt, pass, salt, pass);
    if (mysql_query(&conn, query)) {
        fprintf(stderr, "db_store_user error: %s\n", mysql_error(&conn));
        return -1;
    }
    return 0;
}
//获取用户名和密码
int db_get_user(const char *user, char *salt, char *encrypted_pass) {
    char query[256];
    snprintf(query, sizeof(query), "SELECT salt, encrypted_pass FROM users WHERE username='%s'", user);
    if (mysql_query(&conn, query)) {
        fprintf(stderr, "db_get_user error: %s\n", mysql_error(&conn));
        return -1;
    }
    MYSQL_RES *res = mysql_store_result(&conn);
    if (!res) return -1;
    MYSQL_ROW row = mysql_fetch_row(res);
    if (!row) {
        mysql_free_result(res);
        return -1;
    }
    strcpy(salt, row[0]);
    strcpy(encrypted_pass, row[1]);
    mysql_free_result(res);
    return 0;
}


//初始化子线程的数据库
int db_init_thread(MYSQL *conn,char *username,int*key){
    char sql[256];
    snprintf(sql,sizeof(sql),"idSELECT  FROM files WHERE username = '%s' AND parent_id = %d",username,-1);
    printf("%s\n",sql);
    mysql_query(conn,sql);
    MYSQL_RES *res = mysql_store_result(conn);
    if(!res){
        fprintf(stderr, "db_init_thread error: %s\n", mysql_error(conn));
        return -1;
    }
    MYSQL_ROW row;

    if((row = mysql_fetch_row(res)) != NULL) {
        *key = atoi(row[0]);
    }
    mysql_free_result(res);
}
//注册根目录
int regfileroot(MYSQL *conn,const char *username,int *key,pthread_mutex_t *mutex){
    //注册根目录
    char sql[256] = {0};
    snprintf(sql,sizeof(sql),"INSERT INTO file (username, parent_id,type, path) VALUES ('%s', %d, '%c','/')",username,-1,'d');
    printf("%s\n",sql);
    pthread_mutex_lock(mutex);
    mysql_query(conn,sql);
    *key = mysql_insert_id(conn);
    MYSQL_RES *res = mysql_store_result(conn);
}

